/*
 * Copyright (c) 2022 Proton Technologies AG
 * This file is part of Proton Technologies AG and Proton Mail.
 *
 * Proton Mail is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Proton Mail is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Proton Mail. If not, see <https://www.gnu.org/licenses/>.
 */

package ch.protonmail.android.maildetail.domain.repository

import ch.protonmail.android.mailcommon.domain.model.ConversationId
import ch.protonmail.android.mailmessage.domain.model.MessageId
import me.proton.core.domain.entity.UserId
import javax.inject.Inject
import kotlin.time.Duration.Companion.seconds

class MailDetailRepositoryImpl @Inject constructor(
    private val enqueuer: MarkMessageAndConversationReadWorker.Enqueuer
) : MailDetailRepository {

    override suspend fun markMessageAndConversationReadIfAllRead(
        userId: UserId,
        messageId: MessageId,
        conversationId: ConversationId
    ) {
        // Insert some delay before marking message and conversation as read (if all messages are read)
        // because get conversation api remote call may happen just after we mark as Read and upserts
        // the read status to Unread. This causes wrong read status to be shown in conversation list until
        // the next refresh by the event loop.

        enqueuer.enqueue(
            userId = userId,
            params = MarkMessageAndConversationReadWorker.params(
                userId,
                messageId,
                conversationId
            ),
            initialDelay = 2.seconds
        )
    }
}
